package o;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.database.Cursor;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import com.huawei.appgallery.foundation.application.pkgmanage.model.update.ApkUpgradeInfo;
import com.huawei.appgallery.updatemanager.impl.fileinfo.dao.AppFileInfo;
import com.huawei.hmf.annotation.ApiDefine;
import com.huawei.hmf.annotation.Singleton;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import com.huawei.hwid.core.constants.HwAccountConstants;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

@ApiDefine(uri = czu.class)
@Singleton
public class dae implements czu {
    private final byte[] ʽ = new byte[0];
    private final Map<String, ApkUpgradeInfo> ˊ = new ConcurrentHashMap();
    private final Map<String, Long> ˋ = new ConcurrentHashMap();
    private final List<String> ˎ = new ArrayList();
    private String ˏ = null;
    private final Map<String, ApkUpgradeInfo> ॱ = new ConcurrentHashMap();

    static class a implements Runnable {
        private Map<String, Long> ˊ;
        private Context ˎ;
        private final String ˏ;

        public a(Context context, String str, Map<String, Long> map) {
            this.ˎ = context;
            this.ˏ = str;
            this.ˊ = map;
        }

        public void run() {
            daw.ॱ().ˋ(this.ˏ);
            dav.ॱ().ˋ(this.ˏ);
            dae.ˋ(this.ˎ, this.ˊ);
        }
    }

    static class d implements Runnable {
        private final List ˎ;
        private final Context ˏ;
        private final Map<String, Long> ॱ;

        public d(Context context, List list, Map<String, Long> map) {
            this.ˎ = list;
            this.ˏ = context;
            this.ॱ = map;
        }

        public void run() {
            daw.ॱ().ˏ();
            daw.ॱ().ˊ(this.ˎ);
            dae.ˋ(this.ˏ, this.ॱ);
        }
    }

    static class e implements Runnable {
        private final ArrayList ˏ;

        public e(ArrayList arrayList) {
            this.ˏ = arrayList;
        }

        public void run() {
            dav.ॱ().ˏ();
            dav.ॱ().ˊ(this.ˏ);
        }
    }

    static class f implements Runnable {
        private final List<String> ॱ;

        public f(List<String> list) {
            this.ॱ = list;
        }

        public void run() {
            dan.ˏ().ˎ();
            for (String ˎ : this.ॱ) {
                dan.ˏ().ˎ(ˎ);
            }
        }
    }

    static class g implements Runnable {
        private final ApkUpgradeInfo ॱ;

        public g(ApkUpgradeInfo apkUpgradeInfo) {
            this.ॱ = apkUpgradeInfo;
        }

        public void run() {
            dav.ॱ().ˋ(this.ॱ.ᐝ());
            dav.ॱ().ˏ(this.ॱ);
        }
    }

    static class i implements Runnable {
        private Context ˊ;
        private final Map<String, Long> ˋ;
        private final ApkUpgradeInfo ˎ;

        public i(Context context, ApkUpgradeInfo apkUpgradeInfo, Map<String, Long> map) {
            this.ˊ = context;
            this.ˎ = apkUpgradeInfo;
            this.ˋ = map;
        }

        public void run() {
            daw.ॱ().ˋ(this.ˎ.ᐝ());
            daw.ॱ().ˏ(this.ˎ);
            dae.ˋ(this.ˊ, this.ˋ);
        }
    }

    public void ˏ(Context context, List<? extends ApkUpgradeInfo> list, boolean z) {
        synchronized (this.ʽ) {
            this.ˊ.clear();
            for (ApkUpgradeInfo apkUpgradeInfo : list) {
                this.ˊ.put(apkUpgradeInfo.ᐝ(), apkUpgradeInfo);
                if (((czp) dbn.ˎ(czp.class)).ˎ(context) && ((Long) this.ˋ.get(apkUpgradeInfo.ᐝ())) == null) {
                    Logger.ˎ("UpdateDataManagerImpl", "get package: " + apkUpgradeInfo.ᐝ() + " ,time = " + System.currentTimeMillis());
                    this.ˋ.put(apkUpgradeInfo.ᐝ(), Long.valueOf(System.currentTimeMillis()));
                }
            }
            if (z) {
                ॱ(context.getApplicationContext(), new ArrayList(this.ˊ.values()), this.ˋ);
            }
        }
    }

    public void ˊ(List<? extends ApkUpgradeInfo> list, boolean z) {
        synchronized (this.ʽ) {
            this.ॱ.clear();
            for (ApkUpgradeInfo apkUpgradeInfo : list) {
                this.ॱ.put(apkUpgradeInfo.ᐝ(), apkUpgradeInfo);
            }
            if (z) {
                ˋ(new ArrayList(this.ॱ.values()));
            }
        }
    }

    public List<ApkUpgradeInfo> ˎ(boolean z, int i) {
        List<ApkUpgradeInfo> ˏ;
        synchronized (this.ʽ) {
            ˏ = ˏ(this.ˊ, z, i);
        }
        return ˏ;
    }

    public List<String> ॱ(boolean z, int i) {
        List<String> ॱ;
        synchronized (this.ʽ) {
            ॱ = ॱ(this.ˊ, z, i);
        }
        return ॱ;
    }

    public ApkUpgradeInfo ॱ(String str, boolean z, int i) {
        synchronized (this.ʽ) {
            if (TextUtils.isEmpty(str)) {
                return null;
            } else if (z && this.ˎ.contains(str)) {
                return null;
            } else {
                ApkUpgradeInfo apkUpgradeInfo = (ApkUpgradeInfo) this.ˊ.get(str);
                if (apkUpgradeInfo == null) {
                    return null;
                }
                List arrayList = new ArrayList(1);
                arrayList.add(apkUpgradeInfo);
                if ((i & 1) != 0) {
                    ˏ(arrayList);
                }
                if (arrayList.size() != 1) {
                    return null;
                }
                return apkUpgradeInfo;
            }
        }
    }

    @NonNull
    private List<ApkUpgradeInfo> ˏ(Map<String, ApkUpgradeInfo> map, boolean z, int i) {
        List arrayList = new ArrayList();
        for (Entry entry : map.entrySet()) {
            String str = (String) entry.getKey();
            if (!z || !this.ˎ.contains(str)) {
                arrayList.add((ApkUpgradeInfo) entry.getValue());
            }
        }
        if ((i & 1) != 0) {
            ˏ(arrayList);
        }
        Collections.sort(arrayList, new ApkUpgradeInfo());
        return arrayList;
    }

    public List<String> ˊ(boolean z, int i) {
        List<String> ॱ;
        synchronized (this.ʽ) {
            ॱ = ॱ(this.ॱ, z, i);
        }
        return ॱ;
    }

    @NonNull
    private List<String> ॱ(Map<String, ApkUpgradeInfo> map, boolean z, int i) {
        List<String> arrayList;
        synchronized (this.ʽ) {
            arrayList = new ArrayList();
            for (Entry entry : map.entrySet()) {
                String str = (String) entry.getKey();
                if (!z || !this.ˎ.contains(str)) {
                    ApkUpgradeInfo apkUpgradeInfo = (ApkUpgradeInfo) entry.getValue();
                    List arrayList2 = new ArrayList(1);
                    arrayList2.add(apkUpgradeInfo);
                    if ((i & 1) != 0) {
                        ˏ(arrayList2);
                    }
                    if (arrayList2.size() == 1) {
                        arrayList.add(str);
                    }
                }
            }
        }
        return arrayList;
    }

    public int ˋ(boolean z, int i) {
        int size;
        synchronized (this.ʽ) {
            size = ॱ(this.ˊ, z, i).size();
        }
        return size;
    }

    public List<ApkUpgradeInfo> ˏ(boolean z, int i) {
        List<ApkUpgradeInfo> ˏ;
        synchronized (this.ʽ) {
            ˏ = ˏ(this.ॱ, z, i);
        }
        return ˏ;
    }

    public ApkUpgradeInfo ˋ(String str, boolean z, int i) {
        synchronized (this.ʽ) {
            if (TextUtils.isEmpty(str)) {
                return null;
            } else if (z && this.ˎ.contains(str)) {
                return null;
            } else {
                ApkUpgradeInfo apkUpgradeInfo = (ApkUpgradeInfo) this.ॱ.get(str);
                if (apkUpgradeInfo == null) {
                    return null;
                }
                List arrayList = new ArrayList(1);
                arrayList.add(apkUpgradeInfo);
                if ((i & 1) != 0) {
                    ˏ(arrayList);
                }
                if (arrayList.size() != 1) {
                    return null;
                }
                return apkUpgradeInfo;
            }
        }
    }

    public int ʻ(boolean z, int i) {
        int size;
        synchronized (this.ʽ) {
            size = ॱ(this.ॱ, z, i).size();
        }
        return size;
    }

    public void ˏ(List<ApkUpgradeInfo> list) {
        ॱ((List) list);
    }

    public List<ApkUpgradeInfo> ॱ(int i) {
        List arrayList = new ArrayList();
        synchronized (this.ʽ) {
            ˊ(arrayList, this.ˊ, i);
            ˊ(arrayList, this.ॱ, i);
        }
        return arrayList;
    }

    private void ˊ(List<ApkUpgradeInfo> list, Map<String, ApkUpgradeInfo> map, int i) {
        for (Entry entry : map.entrySet()) {
            if (this.ˎ.contains((String) entry.getKey())) {
                ApkUpgradeInfo apkUpgradeInfo = (ApkUpgradeInfo) entry.getValue();
                List arrayList = new ArrayList(1);
                arrayList.add(apkUpgradeInfo);
                if ((i & 1) != 0) {
                    ˏ(arrayList);
                }
                if (arrayList.size() == 1) {
                    list.add(apkUpgradeInfo);
                }
            }
        }
    }

    public int ˏ(int i) {
        int size;
        synchronized (this.ʽ) {
            size = ॱ(i).size();
        }
        return size;
    }

    public void ˊ(String str) {
        this.ˏ = str;
        euf.ˎ().ˋ("keyappnotificationpackagename", str);
    }

    public String ˎ() {
        if (this.ˏ == null) {
            this.ˏ = euf.ˎ().ॱ("keyappnotificationpackagename", "");
        }
        return this.ˏ;
    }

    public void ˎ(Context context, String str) {
        cei.ˋ(context, "UpdateManager", 1020);
        this.ˏ = "";
        euf.ˎ().ʼ("keyappnotificationpackagename");
    }

    public void ˎ(Map<String, Long> map) {
        this.ˋ.putAll(map);
    }

    public boolean ˏ(String str) {
        boolean contains;
        synchronized (this.ʽ) {
            contains = this.ˎ.contains(str);
        }
        return contains;
    }

    public void ˋ(Context context, String str, boolean z) {
        czi.ˎ.ˋ("UpdateDataManagerImpl", "addIgnore:" + str);
        synchronized (this.ʽ) {
            if (!this.ˎ.contains(str)) {
                this.ˎ.add(str);
                if (((czp) dbn.ˎ(czp.class)).ˎ(context)) {
                    this.ˋ.remove(str);
                }
                if (z) {
                    ॱ(context.getApplicationContext(), str, this.ˋ);
                }
            }
        }
        daa.ˊ(context, false, false);
    }

    public void ॱ(Context context, String str) {
        czi.ˎ.ˋ("UpdateDataManagerImpl", "removeIgnore:" + str);
        synchronized (this.ʽ) {
            this.ˎ.remove(str);
            ˋ(str);
        }
        daa.ˊ(context, false, false);
    }

    public void ॱ(Context context) {
        czi.ˎ.ˋ("UpdateDataManagerImpl", "removeAllIgnore");
        synchronized (this.ʽ) {
            this.ˎ.clear();
            ʻ();
        }
        daa.ˊ(context, false, false);
    }

    public void ˏ(Context context, String str) {
        synchronized (this.ʽ) {
            this.ˊ.remove(str);
            this.ॱ.remove(str);
            if (((czp) dbn.ˎ(czp.class)).ˎ(context)) {
                this.ˋ.remove(str);
            }
            ˊ(context.getApplicationContext(), str, this.ˋ);
        }
        daa.ˊ(context, false, false);
    }

    public void ˊ(Context context, ApkUpgradeInfo apkUpgradeInfo, boolean z) {
        synchronized (this.ʽ) {
            this.ˊ.put(apkUpgradeInfo.ᐝ(), apkUpgradeInfo);
            if (((czp) dbn.ˎ(czp.class)).ˎ(context) && ((Long) this.ˋ.get(apkUpgradeInfo.ᐝ())) == null) {
                Logger.ˎ("UpdateDataManagerImpl", "add package: " + apkUpgradeInfo.ᐝ() + " ,time = " + System.currentTimeMillis());
                this.ˋ.put(apkUpgradeInfo.ᐝ(), Long.valueOf(System.currentTimeMillis()));
            }
            if (z) {
                ˎ(context.getApplicationContext(), apkUpgradeInfo, this.ˋ);
            }
        }
    }

    public void ˋ(ApkUpgradeInfo apkUpgradeInfo, boolean z) {
        synchronized (this.ʽ) {
            this.ॱ.put(apkUpgradeInfo.ᐝ(), apkUpgradeInfo);
            if (z) {
                ˏ(apkUpgradeInfo);
            }
        }
    }

    public ApkUpgradeInfo ˎ(ApkUpgradeInfo apkUpgradeInfo) {
        if (ˋ(apkUpgradeInfo)) {
            return apkUpgradeInfo;
        }
        return null;
    }

    private static boolean ˋ(ApkUpgradeInfo apkUpgradeInfo) {
        if (apkUpgradeInfo == null || TextUtils.isEmpty(apkUpgradeInfo.ᐝ())) {
            return false;
        }
        if (ॱ(apkUpgradeInfo)) {
            return false;
        }
        if (((cdg) cdb.ॱ(cdg.class)).ʽ(apkUpgradeInfo.ᐝ())) {
            czi.ˎ.ˋ("UpdateDataManagerImpl", "app stoped.");
            return false;
        }
        PackageInfo ᐝ = ((cdg) cdb.ॱ(cdg.class)).ᐝ(apkUpgradeInfo.ᐝ());
        if (ᐝ == null) {
            czi.ˎ.ˋ("UpdateDataManagerImpl", "app not installed.");
            return false;
        } else if (ᐝ.versionCode != apkUpgradeInfo.ᐝॱ()) {
            czi.ˎ.ˋ("UpdateDataManagerImpl", "app has error oldVersionCode:" + apkUpgradeInfo.ᐝॱ() + ",installedInfo:" + ᐝ.versionCode);
            return false;
        } else if (ˊ(apkUpgradeInfo)) {
            return false;
        } else {
            if (cdn.ʻ().ᐝ() == 1 || apkUpgradeInfo.ˏˎ() != 1) {
                apkUpgradeInfo.ˎ(ᐝ.versionName == null ? HwAccountConstants.NULL : ᐝ.versionName);
                return true;
            }
            czi.ˎ.ˋ("UpdateDataManagerImpl", "Phone do not support maple,but app is maple,package: " + apkUpgradeInfo.ᐝ());
            return false;
        }
    }

    private static boolean ॱ(ApkUpgradeInfo apkUpgradeInfo) {
        if (apkUpgradeInfo.ˏॱ() == 1 || apkUpgradeInfo.ॱˋ() == 1) {
            PackageInfo ᐝ = ((cdg) cdb.ॱ(cdg.class)).ᐝ(apkUpgradeInfo.ᐝ());
            if (ᐝ != null && cep.ˎ(ᐝ) == 2) {
                czi.ˎ.ˋ("UpdateDataManagerImpl", "app has update,but it is System App Or System Update App,so abort it!!" + apkUpgradeInfo.ᐝ());
                return true;
            }
        }
        return false;
    }

    private static boolean ˊ(ApkUpgradeInfo apkUpgradeInfo) {
        try {
            if (!new URL(apkUpgradeInfo.ˈ()).getPath().endsWith(".apk") && (apkUpgradeInfo.ʼ() <= 0 || apkUpgradeInfo.ʻॱ() <= apkUpgradeInfo.ʼ())) {
                String str = apkUpgradeInfo.ᐝ() + HiAnalyticsConstant.REPORT_VAL_SEPARATOR + apkUpgradeInfo.ʼ() + HiAnalyticsConstant.REPORT_VAL_SEPARATOR + apkUpgradeInfo.ʻॱ();
                czi.ˎ.ˊ("UpdateDataManagerImpl", "online diff size error:" + str);
                dgh.ॱ(ContentManager.getInstant().getContext(), "980101", str);
                return true;
            }
        } catch (MalformedURLException e) {
            czi.ˎ.ˊ("UpdateDataManagerImpl", "url error, " + e.toString());
        }
        return false;
    }

    public void ॱ(List<String> list, boolean z) {
        synchronized (this.ʽ) {
            this.ˎ.clear();
            this.ˎ.addAll(list);
            if (z) {
                ˎ((List) list);
            }
        }
    }

    public Cursor ॱ() {
        return dak.ˏ().ˏ(null, null, null, null);
    }

    public void ˏ(Context context) {
        daf.ॱ(context);
    }

    public void ॱ(String str) {
        dam.ˋ(str);
    }

    public void ˎ(String str) {
        dam.ॱ(str);
    }

    public long ˊ() {
        return daj.ˎ().ˏ();
    }

    public void ˋ() {
        dam.ˋ();
    }

    public void ᐝ() {
        das.ˎ().ˋ();
    }

    public long ʽ() {
        return daj.ˎ().ˊ();
    }

    public List<String> ˏ() {
        List<String> arrayList = new ArrayList();
        for (AppFileInfo appFileInfo : dam.ˊ()) {
            PackageInfo ᐝ = ((cdg) cdb.ॱ(cdg.class)).ᐝ(appFileInfo.ॱ());
            if (ᐝ == null || ᐝ.versionCode != appFileInfo.ˎ()) {
                Logger.ˎ("UpdateDataManagerImpl", "expired File Info:" + appFileInfo.ॱ());
                dam.ˋ(appFileInfo.ॱ());
            } else {
                arrayList.add(appFileInfo.ॱ());
            }
        }
        return arrayList;
    }

    private void ॱ(List<ApkUpgradeInfo> list) {
        Iterator it = list.iterator();
        while (it.hasNext()) {
            ApkUpgradeInfo apkUpgradeInfo = (ApkUpgradeInfo) it.next();
            if (apkUpgradeInfo == null) {
                it.remove();
            } else if (apkUpgradeInfo.ˋ() && dao.ˏ().ˋ() != null && !dao.ˏ().ˋ().ˎ(apkUpgradeInfo.ᐝ())) {
                czi.ˎ.ˋ("UpdateDataManagerImpl", apkUpgradeInfo.ᐝ() + " is not ordered update.");
                it.remove();
            } else if (((cdg) cdb.ॱ(cdg.class)).ʽ(apkUpgradeInfo.ᐝ())) {
                czi.ˎ.ˋ("UpdateDataManagerImpl", "app is stoped:" + apkUpgradeInfo.ᐝ());
                it.remove();
            }
        }
    }

    private void ॱ(Context context, List list, Map<String, Long> map) {
        dso.ˊ.execute(new d(context, list, map));
    }

    private void ˎ(Context context, ApkUpgradeInfo apkUpgradeInfo, Map<String, Long> map) {
        dso.ˊ.execute(new i(context, apkUpgradeInfo, map));
    }

    private void ˋ(ArrayList arrayList) {
        dso.ˊ.execute(new e(arrayList));
    }

    private void ˏ(ApkUpgradeInfo apkUpgradeInfo) {
        dso.ˊ.execute(new g(apkUpgradeInfo));
    }

    private void ˊ(Context context, String str, Map<String, Long> map) {
        dso.ˊ.execute(new a(context, str, map));
    }

    private void ॱ(Context context, String str, Map<String, Long> map) {
        dso.ˊ.execute(new b(context, str, map));
    }

    private void ˋ(String str) {
        dso.ˊ.execute(new j(str));
    }

    private void ʻ() {
        dso.ˊ.execute(new c(null));
    }

    private void ˎ(List<String> list) {
        dso.ˊ.execute(new f(list));
    }

    private static void ˋ(Context context, Map<String, Long> map) {
        Set<Entry> entrySet = map.entrySet();
        Set hashSet = new HashSet(entrySet.size());
        for (Entry entry : entrySet) {
            Long l = (Long) entry.getValue();
            String str = (String) entry.getKey();
            if (l != null) {
                hashSet.add(str + "###" + l.toString());
            }
        }
        new dax(context).ˋ("last_received_update_time_timemillis", hashSet);
    }
}
